package org.jeecg.modules.manager.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.zhyd.oauth.utils.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manager.entity.GoodsType;
import org.jeecg.modules.manager.service.IGoodsTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 商品类型表
 * @Author: jeecg-boot
 * @Date:   2021-11-19
 * @Version: V1.0
 */
@Slf4j
@Api(tags="商品类型表")
@RestController
@RequestMapping("/manager/goodsType")
public class GoodsTypeController extends JeecgController<GoodsType, IGoodsTypeService> {
	@Autowired
	private IGoodsTypeService goodsTypeService;

	@AutoLog(value = "商品类型表-列表查询")
	@ApiOperation(value="商品类型表-列表查询", notes="商品类型表-列表查询,先查出没有父节点商品类型,后面的二你就设计成点一次然后根据id查询,来查出子节点吧")
	@GetMapping(value = "/list")
	public Result<?> queryPageList() {
		return Result.OK(goodsTypeService.quertList());
	}

	/**
	 * 添加
	 *
	 * @param goodsType
	 * @return
	 */
	@AutoLog(value = "商品类型表-添加或修改")
	@ApiOperation(value="商品类型表-添加或修改", notes="商品类型表-添加或修改")
	@PostMapping(value = "/addOrEdit")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> add(@RequestBody GoodsType goodsType) {
		try{
			if (Optional.ofNullable(goodsType.getId()).isPresent() && StringUtils.isNotEmpty(goodsType.getId())) {
				goodsType.setId(UUIDGenerator.generate());
				goodsType.setCreatedBy(currentUser().getUsername());
				goodsType.setCreatedTime(new Date());
				goodsTypeService.save(goodsType);
				return Result.OK("添加成功！");
			}else {
				goodsType.setUpdatedBy(currentUser().getUsername());
				goodsType.setUpdatedTime(new Date());
				goodsTypeService.updateById(goodsType);
				return Result.OK("修改成功！");
			}
		}catch (Exception e){
			return Result.error("修改或更新失败!");
		}

	}


	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品类型表-通过id删除")
	@ApiOperation(value="商品类型表-通过id删除", notes="商品类型表-通过id删除,如果该商品类型有和商品关联,那就不允许删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {

		return goodsTypeService.checkRemoveById(id) == 1 ? Result.OK("删除成功!"):Result.error("删除失败");
	}


	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品类型表-通过id查询")
	@ApiOperation(value="商品类型表-通过id查询", notes="商品类型表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		return Result.OK(goodsTypeService.checkQueryById(id));
	}

  /**
   * 导出excel
   *
   * @param request
   * @param goodsType
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, GoodsType goodsType) {
      return super.exportXls(request, goodsType, GoodsType.class, "商品类型表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, GoodsType.class);
  }

}
